package controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import serivce.House;
import serivce.LoginService;
import serivce.MapService;
import untity.BriefHouse;
import untity.Position;
import untity.screen.Screen;

import javax.servlet.http.HttpServletRequest;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/search")
public class Serach {

    @Autowired
    private LoginService loginService;
    @Autowired
    private MapService mapService;
    @Autowired
    private House house;
    @RequestMapping("/page")
    public String searchPage(HttpServletRequest req){
        return "search";
    }

    @ResponseBody
    @RequestMapping("/data")
    public JSONObject searchPageDate(HttpServletRequest request, String ip){
        HashMap<String,Object> hashMap=loginService.checkSession(request);

        System.out.println("hasMap"+hashMap);
        if (hashMap==null){
            return JSON.parseObject("{'id':0}");
        }
        JSONObject json=new JSONObject(hashMap);

        return json;
    }

    @ResponseBody
    @RequestMapping("/screen")
    public List<BriefHouse> screen(@RequestBody JSONObject jsonObject){
        Screen screen=jsonObject.toJavaObject(Screen.class);
        JSONObject jsonObject1=jsonObject.getJSONObject("form");
        Position position =jsonObject1.toJavaObject(Position.class);
        System.out.println(jsonObject);
        String path=position.getProvince()+position.getCity()+position.getArea()+position.getDetail();
//        JSONObject code = mapService.getPosition(path);
//        System.out.println("conde======="+code);
//        JSONObject result = code.getJSONObject("result");
//        if (result != null) {
//            String level = result.getString("level");
//            JSONObject location = result.getJSONObject("location");
//            position.setLng(location.getString("lng"));
//            position.setLat(location.getString("lat"));
//            position.setLevel(level);
//        }
        System.out.println(jsonObject);
        System.out.println("啊啊"+position+"\n"+screen);
        List<BriefHouse> list=house.screen(position,screen);
        String ip=null;
        try {
//            InetAddress ip4 = Inet4Address.getLocalHost();
//            System.out.println(ip4.getHostAddress());
            InetAddress iphost=InetAddress.getLocalHost();
            ip=iphost.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        String imgpath=null;
        for (BriefHouse b: list ){
            String[] imgs=house.searchImg(b.getId());

            if (imgs.length!=0){
                imgpath=ip+":8081/"+imgs[0];
            }else{
                imgpath=null;
            }
//            System.out.println(imgpath);
            b.setImgPath(imgpath);
            System.out.println(b);
        }

        return list;
    }
}
